<?php

require 'httpful-0.2.0.phar';

use Httpful\Request;

class ServiceClient {

    public $host;

    // urls
    public static $users = 'users';
    public static $cars = 'cars';
    public static $carDetails = 'cars/:vin/details';

    public $response;
    public $request;

    public function __construct($host) {
        $this->host = $host;
    }

    public function getUsers() {
        $response = $this->get(self::$users);
        return $response->body;
    }
    
    private function combineArrays($keys, $values) {
        $data = array();
        foreach($keys as $index => $key) {
            $data[$key] = $values[$index];
        }
        
        return $data;
    }

    public function createUser($email, $fname, $lname, $address, $zipcode, $phoneno, $password) {
        $keys = array('email', 'fname', 'lname', 'address', 'zipcode', 'phoneno', 'password');
        $values = func_get_args();
        
        $data = $this->combineArrays($keys, $values);

        $response = $this->post(self::$users, $data);
        return $response->body;
    }

    public function createCar($email, $vin, $licensePlate, $model) {
        $keys = array('email', 'vin', 'licensePlate', 'model');
        $values = func_get_args();
        
        $data = $this->combineArrays($keys, $values);

        return $this->post(self::$cars, $data)->body;
    }
    
    public function createCarDetails($vin, $noOfKm, $dateService, $kmToService, 
        $kmPrLtr, $oilStatus, $fuelStatus, $lightStatus, $doorsStatus, $fuelUsedTrip) {
        
        $data = array(
            'noOfKm' => $noOfKm, 
            'dateService' => $dateService, 
            'kmToService' => $kmToService, 
            'kmPrLtr' => $kmPrLtr, 
            'oilStatus' => $oilStatus, 
            'fuelStatus' => $fuelStatus, 
            'lightStatus' => $lightStatus, 
            'doorsStatus' => $doorsStatus, 
            'fuelUsedTrip' => $fuelUsedTrip
        );
        
        $url = self::$carDetails;
        $url = str_replace(':vin', $vin, $url);
        
        return $this->post($url, $data)->body;
    }

    public function get($path) {
        $url = $this->getUrl($path);
        return Request::get($url)->expectsJson()->send();
    }

    public function post($path, $data) {
        $url = $this->getUrl($path);
        $this->request = Request::post($url)->body($data)->sendsForm();
        $this->response = $this->request->send();
        return $this->response;
    }
    
    public function put($path, $data) {
        $url = $this->getUrl($path);
        $this->request = Request::put($url)->body($data)->sendsForm();
        $this->response = $this->request->send();
        return $this->response;
    }

    private function getUrl($path) {
        $url = "http://{$this->host}/{$path}?html_errors=false";
        return $url;
    }
    
    public function getDetails($vin) {
        $path = self::$carDetails;
        $path = str_replace(':vin', $vin, $path);
        return $this->get($path)->body;
    }

    private function getCarDetailPath($vin) {
        return str_replace(':vin', $vin, self::$carDetails);
    }

    public function updateCarDetails($vin, $data) {
        $path = $this->getCarDetailPath($vin);
        return $this->put($path, $data)->body;
    }

}
